package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Task;
import cn.stylefeng.guns.sys.modular.system.entity.TaskFlow;
import cn.stylefeng.guns.sys.modular.system.mapper.TaskFlowMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.TaskFlowParam;
import cn.stylefeng.guns.sys.modular.system.model.result.TaskFlowResult;
import cn.stylefeng.guns.sys.modular.system.service.ProjectService;
import cn.stylefeng.guns.sys.modular.system.service.TaskFlowService;
import cn.stylefeng.guns.sys.modular.system.service.TaskService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-09-10
 */
@Service
public class TaskFlowServiceImpl extends ServiceImpl<TaskFlowMapper, TaskFlow> implements TaskFlowService {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private TaskService taskService;

    @Override
    public void add(TaskFlowParam param){
        if(ToolUtil.isEmpty(param.getTaskName())){
            Task task = taskService.getById(param.getTaskId());
            param.setTaskName(task.getName());
        }
        TaskFlow entity = getEntity(param);
        if (ToolUtil.isEmpty(param.getCreateUser())){
            entity.setCreateUser(LoginContextHolder.getContext().getUserId());
        }
        this.save(entity);
    }

    @Override
    public void addH5(TaskFlowParam param,Long userId){
        if(ToolUtil.isEmpty(param.getTaskName())){
            Task task = taskService.getById(param.getTaskId());
            param.setTaskName(task.getName());
        }
        TaskFlow entity = getEntity(param);
        if (ToolUtil.isEmpty(param.getCreateUser())){
            entity.setCreateUser(userId);
        }
        this.save(entity);
    }

    @Override
    public void delete(TaskFlowParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TaskFlowParam param){
        TaskFlow oldEntity = getOldEntity(param);
        TaskFlow newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TaskFlowResult findBySpec(TaskFlowParam param){
        return null;
    }

    @Override
    public List<TaskFlowResult> findListBySpec(TaskFlowParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(TaskFlowParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(TaskFlowParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private TaskFlow getOldEntity(TaskFlowParam param) {
        return this.getById(getKey(param));
    }

    private TaskFlow getEntity(TaskFlowParam param) {
        TaskFlow entity = new TaskFlow();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public boolean isExist(TaskFlowParam param){
        QueryWrapper<TaskFlow> q= new QueryWrapper<>();
        q.eq("pro_id",param.getProId()).eq("task_id",param.getTaskId()).eq("status",param.getStatus());
        List<TaskFlow> flows = this.baseMapper.selectList(q);
        if(flows.size()>0){
            return true;
        }
        return false;
    }

}
